/*
  isnaninf.h

% Version:  v0.7f
% Build:    8110521
% Date:     Nov-05 2008, 9:00 PM CET
% Author:   Jochen Weber, SCAN Unit, Columbia University, NYC, NY, USA
% URL/Info: http://wiki.brainvoyager.com/BVQXtools

*/


/* this is a "cheaper" test for isinf/isnan */


/* IsInf / IsNaN combined */
#define IS_BAD_VAL()                                                    \
    (((*isbadptr1 == posinfval1)  &&                                    \
      (*isbadptr2 == posinfval2)) ||                                    \
     ((*isbadptr1 == neginfval1)  &&                                    \
      (*isbadptr2 == neginfval2)) ||                                    \
     ((*isbadptr1 == nanval1)     &&                                    \
      (*isbadptr2 == nanval2)))

#define IS_GOOD_VAL()                                                   \
    (((*isbadptr1 != posinfval1)  ||                                    \
      (*isbadptr2 != posinfval2)) &&                                    \
     ((*isbadptr1 != neginfval1)  ||                                    \
      (*isbadptr2 != neginfval2)) &&                                    \
     ((*isbadptr1 != nanval1)     ||                                    \
      (*isbadptr2 != nanval2)))

/* IsInf */
#define IS_INF()                                                        \
    (((*isbadptr1 == posinfval1)  &&                                    \
      (*isbadptr2 == posinfval2)) ||                                    \
     ((*isbadptr1 == neginfval1)  &&                                    \
      (*isbadptr2 == neginfval2)))

/* IsNaN */
#define IS_NAN()                                                        \
    (((*isbadptr1 == nanval1)  &&                                       \
      (*isbadptr2 == nanval2)))

/* replacement for mxIsInf / mxIsNaN */
#define IF_IS_BAD_VAL(VAL)                                              \
    isbadval = (double) VAL;                                            \
    if (IS_BAD_VAL())

/* replacement for !mxIsInf && !mxIsNaN */
#define IF_IS_GOOD_VAL(VAL)                                             \
    isbadval = (double) VAL;                                            \
    if (IS_GOOD_VAL())

/* replacement for mxIsInf */
#define IF_IS_INF(VAL)                                                  \
    isbadval = (double) VAL;                                            \
    if (IS_INF())

/* replacement for mxIsNaN */
#define IF_IS_NAN(VAL)                                                  \
    isbadval = (double) VAL;                                            \
    if (IS_NAN())

/* initialization of memory for isinf/isnan test */
#define INIT_INF_NAN_BAD_VAL()                                          \
    isbadptr1 = (unsigned long *) &isbadval;                            \
    isbadptr2 = &isbadptr1[1];                                          \
    isbadval = mxGetInf();                                              \
    posinfval1 = *isbadptr1;                                            \
    posinfval2 = *isbadptr2;                                            \
    isbadval = -isbadval;                                               \
    neginfval1 = *isbadptr1;                                            \
    neginfval2 = *isbadptr2;                                            \
    isbadval = mxGetNaN();                                              \
    nanval1 = *isbadptr1;                                               \
    nanval2 = *isbadptr2;

/* required variables */
#define VARS_FOR_ISINFNAN                                               \
    double isbadval = 0.0;                                              \
    unsigned long                                                       \
        *isbadptr1,                                                     \
        *isbadptr2,                                                     \
        posinfval1,                                                     \
        posinfval2,                                                     \
        neginfval1,                                                     \
        neginfval2,                                                     \
        nanval1,                                                        \
        nanval2;
